package com.secneo.appshare.threads;

import java.io.File;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.OtherUserApps;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.AppInfo;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.common.Util;
import com.secneo.appshare.database.AppFile;
import com.secneo.appshare.database.AppShareDatabaseHelper;

public class UserAppThread extends Thread {
	
	private Context ctx;
	private Handler handler;
	private  ArrayList<AppInfo> otherUserApp;
	private OtherUserApps myStatusInfo;
	 List<ApplicationInfo> filteredApps =null;
	 ArrayList<AppInfo> appInfoList=new ArrayList<AppInfo>();
	 int orderAsc=-1;
	 int orderFlag=0;
	public UserAppThread(Context ctx, Handler handler, String uid, String other_uid,int orderFlag,int orderAsc) {
		this.ctx = ctx;
		this.handler = handler;
		this.orderAsc=orderAsc;
		this.orderFlag=orderFlag;
		 PackageManager pm = ctx.getPackageManager();
		List<ApplicationInfo> allApps = pm.getInstalledApplications(0);
		filteredApps = Util.filterUserApps(allApps);
	}
	
	public  ArrayList<AppInfo>  getUserApps() {
		return otherUserApp;
	}
	
	public OtherUserApps getMyStatusInfo() {
		return myStatusInfo;
	}
	
	public void run() 
	{
		try 
		{
			
			
			AppShareDatabaseHelper adh=new AppShareDatabaseHelper(ctx);
			ArrayList<AppFile> appFileList=adh.getAppFiles(adh);
			for(int i=0;i<filteredApps.size();i++)
			{
				AppInfo appInfo=new AppInfo();
				ApplicationInfo ai=filteredApps.get(i);
				appInfo.appInfo=ai;
			  for(int j=0;j<appFileList.size();j++)
			  {
				  AppFile af=appFileList.get(j);
				  if(ai.packageName.equals(af.PackageName))
				  {
					  appInfo.appCount=af.getRunCount();
					  appInfo.appHidden=af.getHidden();
				  }
			  }
			  appInfoList.add(appInfo);
			}
			otherUserApp=appInfoList;
			if(orderFlag>0)
			{
			reOrder(orderFlag,orderAsc);
			}
			Message msg = new Message();
			msg.what = Constants.MSG_GET_OTHER_USER_STATUS_OK;
			handler.sendMessage(msg);
			
			String uid = LocalStore.getLocaleStoreInstance(ctx).getUid();
			RemoteApiImpl remoteApi = new RemoteApiImpl();
			myStatusInfo = remoteApi.getOtherUserApps(ctx, uid, uid);
			if (otherUserApp != null) 
			{
				Message msg2 = new Message();
				msg2.what = Constants.MSG_GET_MY_STATUS_INFO_OK;
				handler.sendMessage(msg2);
			}
			return;
			
		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	synchronized void reOrder(int type, final int direction) {
		switch (type) {
		case 1:
			Collections.sort(appInfoList, new RunCountComparator(direction,direction));
			break;
		case 2:
			Collections.sort(appInfoList, new DateComparator(direction,ctx));
			break;
		}
		
	}
	
}

/**
 * RunCountComparator
 */
final class RunCountComparator implements Comparator<AppInfo> {

	int type;
	int direction;

	RunCountComparator(int type, int direction) {
		this.type = type;
		this.direction = direction;
	}

	public int compare(AppInfo obj1, AppInfo obj2) {

		return (obj1.appCount == obj2.appCount ? 0 : (obj1.appCount < obj2.appCount ? -1 : 1))*direction;
	}
}
/**
 *DateComparator
 */
final class DateComparator implements Comparator<AppInfo> {
	private PackageManager pm;
	Collator clt = Collator.getInstance();
	int direction;
	Context context;

	DateComparator(int direction,Context context) {
		this.direction = direction;
		this.context=context;
	}


			public int compare( AppInfo obj1,
					AppInfo obj2 )
			{
				long d1 = 0;
				long d2 = 0;

				if ( obj1.appInfo.sourceDir != null )
				{
					d1 = new File( obj1.appInfo.sourceDir ).lastModified( );
				}
				if ( obj2.appInfo.sourceDir != null )
				{
					d2 = new File( obj2.appInfo.sourceDir ).lastModified( );
				}

				return ( d1 == d2 ? 0 : ( d1 < d2 ? -1 : 1 ) )
						* direction;
			}
		} 
/**
 * NameComparator
 *//*
final class NameComparator implements Comparator<ApplicationInfo> {
	Collator clt = Collator.getInstance();
	int direction;

	NameComparator(int direction) {
		this.direction = direction;
	
	}

	public int compare(ApplicationInfo obj1, ApplicationInfo obj2) {
	
	 System.out.println("1111111111111111111111111111111111111111111111");
		File f = new File(obj1.sourceDir);
		 Date date = new Date(f.lastModified());
		 File f1 = new File(obj1.sourceDir);
		 Date date1 = new Date(f1.lastModified());
		 
		return clt.compare(date, date1) * direction;

	}
}
*/